/******************************************************************************/
/* IRQ.C: IRQ Handler                                                         */
/******************************************************************************/
/* This file is part of the uVision/ARM development tools.                    */
/* Copyright (c) 2005-2006 Keil Software. All rights reserved.                */
/* This software may only be used under the terms of a valid, current,        */
/* end user licence from KEIL for a compatible version of KEIL software       */
/* development tools. Nothing else gives you the right to use this software.  */
/******************************************************************************/

#include <91x_lib.h>
#include <RTL.h>
#include "LCD.h"
#include <stdlib.h>
#include <stdio.h>

short Potentiometer, SlideSensor;
extern OS_MBX p_box;
extern OS_MBX s_box;

unsigned char AD_in_progress;          /* AD conversion in progress flag     */

__irq void ADC_IRQ_Handler (void) {    /* AD converter interrupt routine     */
    short *p_msg = malloc(sizeof(short) * 2);
    short *s_msg = p_msg + 1; /* cannot declare 2 heap memory */
    // char pbuffer[12];
    // char sbuffer[12];
    Potentiometer = ADC->DR0 & 0x03FF; /* AD value for global usage (10 bit) */
    SlideSensor = ADC->DR1 & 0x03FF;   /* AD value for global usage (10 bit) */ //dr4.1
    *p_msg = Potentiometer;
    *s_msg = SlideSensor;
    if (isr_mbx_check(p_box) != 0) {
        isr_mbx_send(p_box, p_msg);
    }
    if (isr_mbx_check(s_box) != 0) {
        isr_mbx_send(s_box, s_msg);
    }
    // sprintf (pbuffer, "%d", *p_msg);
    // sprintf (sbuffer, "%d", *s_msg);
    // LCD_cls();
    // LCD_puts(pbuffer);
    // LCD_gotoxy(1,2);
    // LCD_puts(sbuffer);
    // LCD_cur_off();
    
    ADC->CR &= 0xFFFE;                /* Clear STR bit (Start Conversion)   */
    ADC->CR &= 0x7FFF;                /* Clear End of Conversion flag       */

    VIC0->VAR = 0;                    /* Acknowledge Interrupt              */  
    VIC1->VAR = 0;

    AD_in_progress = 0;               /* Clear flag, as AD conv finished    */
}
